﻿
using System ;
using System.Collections.Generic ;

using MskEdit.Sdk ;


namespace MskEdit.Dxf.Model
{

    public class DxfHeader
    {
        #region Private data

        private string[]                    allLines_ ;
        private SdkSubrange                 subrange_ ;
        private Dictionary<string, object>  variables_ ;
 
 
        private int tryGetVariableLocation( string variableName, string [] allLines, SdkSubrange subrange, int fallbackValue ) {
        
            for ( int i = subrange.Begin; i < subrange.End; ++i ) {
            
                if ( variableName.Equals( allLines[i] ) )
                    return i ;
            }
                    
            return fallbackValue ;
        }

        private string tryGetVariableValue( string variableName, string fallbackValue ) {
        
            int i = tryGetVariableLocation( variableName, allLines_, subrange_, -99 ) ;
            
            if ( i < 0 )
                return fallbackValue ;
                
            return allLines_[i+2].Trim() ;
        }
        
        #endregion

        #region Public foundation

        public DxfHeader( string[] allLines, SdkSubrange subrange )
        {
            allLines_ = allLines ;
            subrange_ = subrange ;
            
            variables_ = new Dictionary<string, object>() ;
        }
        
        #endregion

        #region Public modifiers

        public void AddVariable( DxfVariable variable ) {
            variables_[variable.Name] = variable.Value ;
        }
        
        public string this[string varName] {
            get { return tryGetVariableValue( varName, "" ) ; }
        }

        #endregion

    }
}
